function HandleDialogSearchResult(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;

  this._query = (options && options.query) ? options.query : '';
  this._popupBox = (options && options.popupBox) ? options.popupBox : null;
  this._callbackFunc = (options && options.callbackFunc) ? options.callbackFunc : null;
  this._php = (options && options.php) ? options.php : '{{$smarty.const.MYSQL|escape:'javascript'}}';
  this._isTask = (options && options.isTask) ? options.isTask : false;
  
  this._createElements();
};

HandleDialogSearchResult.prototype._createElements = function() {
  var _self = this;
  tr = this._gui.mains.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.height = '24px';
  td.style.textAlign = 'left';
  td.style.padding = '5px 2px';
  td.appendChild(document.createTextNode('Loading...'));
  this._loadData();
};

HandleDialogSearchResult.prototype._loadData = function() {
  this._results = null;
  this._retrieveUsers();
};

HandleDialogSearchResult.prototype._verifyData = function() {
  if (this._results) {
    this._updateElements();
  }
};

HandleDialogSearchResult.prototype._retrieveUsers = function() {
  var _self = this;
  var args = '&sql=' + encodeURIComponent(this._query);

  new RequestUtils()._mysql('findByRequest', args, function(result, params) { _self._results = result.data;
                                                                              _self._verifyData.call(_self);
                                                                            }, {php: this._php});
};

HandleDialogSearchResult.prototype._updateElements = function() {
  var tr, td, _self = this;
  
  DOMUtils.removeTableRows(this._gui.mains, 0);
  
  if (this._isTask) {
    states = [];
    models = [];
    for (var m = 0, ml = this._results.length; m < ml; m++) {
      var object = this._results[m]; exist = false;
      for (var n = 0, nl = models.length; n < nl; n++) {
        var mol = models[n];
        if (mol == object.model) {
          exist = true;
        }
      }
      if (!exist) {
        models[models.length] = object.model;
      }
    }
    
    for (var i = 0, il = models.length; i < il; i++) {
      var object = models[i];
      model = Manage.map()[object];
      for (var j = 0, jl = model.states.length; j < jl; j++) {
        var s = model.states[j];
        states[states.length] = s;
      }
    }
    
    var results = [];
    for (var i = 0, il = states.length; i < il; i++) {
      var object = states[i]; exist = false;
      for (var j = 0, jl = results.length; j < jl; j++) {
        var result = results[j];
        if (result == object.title) {
          exist = true;
        }
      }
      if (!exist && object.title.indexOf(this._item.value) != -1) {
        results[results.length] = object.title;
      }
    }
    if (results.length == 0) {
      this._popupBox._close();
    } else {
      for (j = 0, jl = results.length; j < jl; j++) {
        var res = results[j];
        tr = this._gui.mains.insertRow(-1);
        td = tr.insertCell(-1);
        td.style.height = '24px';
        td.style.textAlign = 'left';
        td.style.padding = '5px 2px';
        td.style.cursor = 'default';
        
        var index = -1;
        if (res.toLowerCase().indexOf(this._item.value.toLowerCase()) != -1) {
          index = res.toLowerCase().indexOf(this._item.value.toLowerCase());
        }
        
        str0 = res.substring(0, index);
        str1 = res.substring(index, index + this._item.value.length);
        str2 = res.substring(index + this._item.value.length);
        
        span0 = document.createElement('span');
        span0.appendChild(document.createTextNode(str0));
        td.appendChild(span0);
  
        span1= document.createElement('span');
        span1.style.fontWeight = (index == -1) ? 'regular' : 'bold';
        span1.style.color = '#0000ff';
        span1.appendChild(document.createTextNode(str1));
        td.appendChild(span1);
      
        span2= document.createElement('span');
        span2.appendChild(document.createTextNode(str2));
        td.appendChild(span2);
        
        td._obj = res;
        td.onmouseover = function() {this.style.backgroundColor = '#f5ebe1';};
        td.onmouseout = function() {this.style.backgroundColor = '#fff';};
        td.onclick = function() { _self._item.value = this._obj;
                                  _self._popupBox._close();
                                  _self._callbackFunc();
                                };
      }
    }
  } else {
  
    var results = [];
    for (var i = 0, il = this._results.length; i < il; i++) {
      var object = this._results[i];
      for (attr in object) {
        exist = false;
        for (var j = 0, jl = results.length; j < jl; j++) {
          var result = results[j];
          if (object[attr] == result) {
            exist = true;
          }
        }
        if (object[attr].indexOf(this._item.value) != -1 && !exist) {
          results[results.length] = object[attr];
        }
      }
    }
    if (results.length == 0) {
      this._popupBox._close();
    } else {
      for (j = 0, jl = results.length; j < jl; j++) {
        var res = results[j];
        tr = this._gui.mains.insertRow(-1);
        td = tr.insertCell(-1);
        td.style.height = '24px';
        td.style.textAlign = 'left';
        td.style.padding = '5px 2px';
        td.style.cursor = 'default';
        
        var index = -1;
        if (res.toLowerCase().indexOf(this._item.value.toLowerCase()) != -1) {
          index = res.toLowerCase().indexOf(this._item.value.toLowerCase());
        }
        
        str0 = res.substring(0, index);
        str1 = res.substring(index, index + this._item.value.length);
        str2 = res.substring(index + this._item.value.length);
        
        span0 = document.createElement('span');
        span0.appendChild(document.createTextNode(str0));
        td.appendChild(span0);
  
        span1= document.createElement('span');
        span1.style.fontWeight = (index == -1) ? 'regular' : 'bold';
        span1.style.color = '#0000ff';
        span1.appendChild(document.createTextNode(str1));
        td.appendChild(span1);
      
        span2= document.createElement('span');
        span2.appendChild(document.createTextNode(str2));
        td.appendChild(span2);
        
        td._obj = res;
        td.onmouseover = function() {this.style.backgroundColor = '#f5ebe1';};
        td.onmouseout = function() {this.style.backgroundColor = '#fff';};
        td.onclick = function() { _self._item.value = this._obj;
                                  _self._popupBox._close();
                                  _self._callbackFunc();
                                };
      }
    }
  }
};

